<template>
  <div class="api-tool">
    <!-- 左侧历史记录 -->
    <div class="sidebar">
      <h3>历史记录</h3>
      
      <div v-if="savedRequests.length === 0" class="no-history">
        暂无保存的API记录
      </div>
      
      <div v-else class="history-list">
        <div 
          v-for="(item, index) in savedRequests" 
          :key="index"
          class="history-item"
          @click="loadFromHistory(item)"
        >
          <div class="history-header">
            <span class="history-name" :title="item.name">{{ item.alias || item.name }}</span>
            <span class="history-method">{{ item.method }}</span>
          </div>
          <div class="history-url" :title="item.url">{{ item.url }}</div>
          <div class="history-time">{{ formatTime(item.timestamp) }}</div>
          <div class="history-actions">
            <button @click.stop="editAlias(index)" class="edit-btn" title="编辑别名">✏️</button>
            <button @click.stop="deleteFromHistory(index)" class="delete-btn" title="删除">🗑️</button>
          </div>
        </div>
      </div>
      
      <div class="sidebar-actions">
        <button @click="clearHistory" v-if="savedRequests.length > 0" class="clear-history-btn">清空历史</button>
      </div>
    </div>
    
    <!-- 右侧主要内容 -->
    <div class="main-content">
      <div class="request-section">
        <h3>API 调试工具</h3>
      
      <!-- 请求配置 -->
      <div class="request-config">
        <div class="method-url-row">
          <select v-model="requestMethod" class="method-select">
            <option value="GET">GET</option>
            <option value="POST">POST</option>
            <option value="PUT">PUT</option>
            <option value="DELETE">DELETE</option>
            <option value="PATCH">PATCH</option>
            <option value="HEAD">HEAD</option>
            <option value="OPTIONS">OPTIONS</option>
          </select>
          <input 
            v-model="requestUrl" 
            type="text" 
            placeholder="请输入API地址，例如: https://api.example.com/users"
            class="url-input"
          />
          <button @click="sendRequest" :disabled="isLoading" class="send-btn">
            {{ isLoading ? '发送中...' : '发送请求' }}
          </button>
        </div>
        
        <!-- 请求头 -->
        <div class="headers-section">
          <h4>请求头 (Headers)</h4>
          <div class="header-item" v-for="(header, index) in requestHeaders" :key="index">
            <input 
              v-model="header.key" 
              type="text" 
              placeholder="Header名称"
              class="header-key"
            />
            <input 
              v-model="header.value" 
              type="text" 
              placeholder="Header值"
              class="header-value"
            />
            <button @click="removeHeader(index)" class="remove-btn">删除</button>
          </div>
          <button @click="addHeader" class="add-btn">添加Header</button>
        </div>
        
        <!-- 请求体 -->
        <div class="body-section" v-if="['POST', 'PUT', 'PATCH'].includes(requestMethod)">
          <h4>请求体 (Body)</h4>
          <div class="body-type-tabs">
            <button 
              v-for="type in bodyTypes" 
              :key="type.value"
              @click="bodyType = type.value"
              :class="{ active: bodyType === type.value }"
              class="tab-btn"
            >
              {{ type.label }}
            </button>
          </div>
          
          <div v-if="bodyType === 'json'" class="json-body">
            <textarea 
              v-model="requestBody" 
              placeholder="请输入JSON格式的请求体，例如：{&quot;name&quot;: &quot;张三&quot;, &quot;age&quot;: 25}"
              class="body-textarea"
            ></textarea>
          </div>
          
          <div v-else-if="bodyType === 'form'" class="form-body">
            <div class="form-item" v-for="(item, index) in formData" :key="index">
              <input 
                v-model="item.key" 
                type="text" 
                placeholder="字段名"
                class="form-key"
              />
              <input 
                v-model="item.value" 
                type="text" 
                placeholder="字段值"
                class="form-value"
              />
              <button @click="removeFormItem(index)" class="remove-btn">删除</button>
            </div>
            <button @click="addFormItem" class="add-btn">添加字段</button>
          </div>
          
          <div v-else-if="bodyType === 'text'" class="text-body">
            <textarea 
              v-model="requestBody" 
              placeholder="请输入纯文本内容"
              class="body-textarea"
            ></textarea>
          </div>
        </div>
      </div>
      
      <!-- 快速操作 -->
      <div class="quick-actions">
        <button @click="clearRequest" class="clear-btn">清空请求</button>
        <button @click="showSaveDialog = true" :disabled="!lastResponse" class="save-btn">保存到历史</button>
      </div>
      
      <!-- 跨域说明 -->
       <div class="cors-info">
         <p class="cors-tip">
           <strong>跨域解决方案：</strong>
           ✅ jsonplaceholder.typicode.com 和 httpbin.org 已配置专用代理<br>
           ✅ 其他外部API自动尝试直接请求，失败时自动切换到代理模式<br>
           ✅ 支持所有HTTP方法和自定义请求头<br>
           💡 如遇到跨域问题，工具会自动处理并显示相应提示
         </p>
       </div>
    </div>
    
    <!-- 响应区域 -->
    <div class="response-section" v-if="lastResponse || isLoading">
      <h3>响应结果</h3>
      
      <div v-if="isLoading" class="loading">
        <div class="loading-spinner"></div>
        <span>正在发送请求...</span>
      </div>
      
      <div v-else-if="lastResponse" class="response-content">
        <!-- 响应状态 -->
        <div class="response-status">
          <span class="status-code" :class="getStatusClass(lastResponse.status)">{{ lastResponse.status }}</span>
          <span class="status-text">{{ lastResponse.statusText }}</span>
          <span class="response-time">{{ lastResponse.responseTime }}ms</span>
        </div>
        
        <!-- 响应头 -->
        <div class="response-headers">
          <h4>响应头</h4>
          <div class="headers-display">
            <div v-for="(value, key) in lastResponse.headers" :key="key" class="header-row">
              <span class="header-name">{{ key }}:</span>
              <span class="header-value">{{ value }}</span>
            </div>
          </div>
        </div>
        
        <!-- 响应体 -->
        <div class="response-body">
          <h4>响应体</h4>
          <div class="response-actions">
            <button @click="copyResponse" class="copy-btn">复制响应</button>
            <button @click="formatJson" v-if="isJsonResponse" class="format-btn">格式化JSON</button>
          </div>
          <pre class="response-text">{{ formattedResponse }}</pre>
        </div>
      </div>
    </div>
    
    </div>
    
    <!-- 保存对话框 -->
    <div v-if="showSaveDialog" class="modal-overlay" @click="showSaveDialog = false">
      <div class="modal-content" @click.stop>
        <h3>保存API记录</h3>
        <input 
          v-model="saveName" 
          type="text" 
          placeholder="请输入记录名称"
          class="save-name-input"
          @keyup.enter="saveToHistory"
        />
        <input 
          v-model="saveAlias" 
          type="text" 
          placeholder="请输入别名（可选）"
          class="save-alias-input"
          @keyup.enter="saveToHistory"
        />
        <div class="modal-actions">
          <button @click="saveToHistory" :disabled="!saveName.trim()" class="confirm-btn">保存</button>
          <button @click="showSaveDialog = false" class="cancel-btn">取消</button>
        </div>
      </div>
    </div>
    
    <!-- 编辑别名对话框 -->
    <div v-if="showEditDialog" class="modal-overlay" @click="showEditDialog = false">
      <div class="modal-content" @click.stop>
        <h3>编辑别名</h3>
        <input 
          v-model="editingAlias" 
          type="text" 
          placeholder="请输入新别名"
          class="save-alias-input"
          @keyup.enter="updateAlias"
        />
        <div class="modal-actions">
          <button @click="updateAlias" class="confirm-btn">保存</button>
          <button @click="showEditDialog = false" class="cancel-btn">取消</button>
        </div>
      </div>
    </div>
    
    <!-- 消息提示 -->
    <div v-if="message" :class="['message', { success: isSuccess, error: !isSuccess }]">
      {{ message }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'

// 响应式数据
const requestMethod = ref('GET')
const requestUrl = ref('')
const requestHeaders = ref([{ key: 'Content-Type', value: 'application/json' }])
const requestBody = ref('')
const bodyType = ref('json')
const formData = ref([{ key: '', value: '' }])
const isLoading = ref(false)
const lastResponse = ref<any>(null)
const message = ref('')
const isSuccess = ref(false)
const showSaveDialog = ref(false)
const saveName = ref('')
const saveAlias = ref('')
const savedRequests = ref<any[]>([])
const showEditDialog = ref(false)
const editingAlias = ref('')
const editingIndex = ref(-1)

// 请求体类型
const bodyTypes = [
  { label: 'JSON', value: 'json' },
  { label: 'Form Data', value: 'form' },
  { label: 'Text', value: 'text' }
]

// 计算属性
const isJsonResponse = computed(() => {
  if (!lastResponse.value) return false
  const contentType = lastResponse.value.headers['content-type'] || ''
  return contentType.includes('application/json')
})

const formattedResponse = computed(() => {
  if (!lastResponse.value) return ''
  
  if (isJsonResponse.value) {
    try {
      const parsed = JSON.parse(lastResponse.value.data)
      return JSON.stringify(parsed, null, 2)
    } catch {
      return lastResponse.value.data
    }
  }
  
  return lastResponse.value.data
})

// 添加请求头
function addHeader() {
  requestHeaders.value.push({ key: '', value: '' })
}

// 删除请求头
function removeHeader(index: number) {
  requestHeaders.value.splice(index, 1)
}

// 添加表单字段
function addFormItem() {
  formData.value.push({ key: '', value: '' })
}

// 删除表单字段
function removeFormItem(index: number) {
  formData.value.splice(index, 1)
}

// 发送请求
async function sendRequest() {
  if (!requestUrl.value.trim()) {
    showMessage('请输入API地址', false)
    return
  }
  
  isLoading.value = true
  const startTime = Date.now()
  
  try {
    // 构建请求头
    const headers: Record<string, string> = {}
    requestHeaders.value.forEach(header => {
      if (header.key.trim() && header.value.trim()) {
        headers[header.key.trim()] = header.value.trim()
      }
    })
    
    // 构建请求体
    let body: string | FormData | undefined
    if (['POST', 'PUT', 'PATCH'].includes(requestMethod.value)) {
      if (bodyType.value === 'json') {
        body = requestBody.value
      } else if (bodyType.value === 'form') {
        const formDataObj = new FormData()
        formData.value.forEach(item => {
          if (item.key.trim() && item.value.trim()) {
            formDataObj.append(item.key.trim(), item.value.trim())
          }
        })
        body = formDataObj
        delete headers['Content-Type'] // 让浏览器自动设置
      } else if (bodyType.value === 'text') {
        body = requestBody.value
      }
    }
    
    // 发送请求 - 增强跨域解决方案
    let finalUrl = requestUrl.value
    const fetchOptions: RequestInit = {
      method: requestMethod.value,
      headers: { ...headers },
      body,
      cache: 'no-cache',
      redirect: 'follow'
    }
    
    // 检测是否需要使用代理（开发环境或桌面应用）
    const needsProxy = import.meta.env.DEV || window.location.protocol === 'wails:'
    
    if (needsProxy) {
      if (requestUrl.value.startsWith('http') && 
                 !requestUrl.value.startsWith('http://localhost') && 
                 !requestUrl.value.startsWith('http://127.0.0.1')) {
        // 对于所有外部API，使用本地代理服务器
        finalUrl = `http://localhost:3001/proxy?url=${encodeURIComponent(requestUrl.value)}`
        showMessage('使用代理服务器发送请求...', true)
      } else {
        // 本地请求直接发送
        fetchOptions.mode = 'cors'
        fetchOptions.credentials = 'omit'
      }
    } else {
      // 生产环境直接请求
      fetchOptions.mode = 'cors'
      fetchOptions.credentials = 'omit'
    }
    
    const response = await fetch(finalUrl, fetchOptions)
    
    const responseTime = Date.now() - startTime
    const responseText = await response.text()
    
    // 获取响应头
    const responseHeaders: Record<string, string> = {}
    response.headers.forEach((value, key) => {
      responseHeaders[key] = value
    })
    
    lastResponse.value = {
      status: response.status,
      statusText: response.statusText,
      headers: responseHeaders,
      data: responseText,
      responseTime
    }
    
    showMessage('请求发送成功', true)
  } catch (error) {
    showMessage(`请求失败: ${(error as Error).message}`, false)
    lastResponse.value = null
  } finally {
    isLoading.value = false
  }
}

// 获取状态码样式
function getStatusClass(status: number) {
  if (status >= 200 && status < 300) return 'success'
  if (status >= 400 && status < 500) return 'client-error'
  if (status >= 500) return 'server-error'
  return 'info'
}

// 复制响应
function copyResponse() {
  if (lastResponse.value) {
    navigator.clipboard.writeText(lastResponse.value.data).then(() => {
      showMessage('响应内容已复制到剪贴板', true)
    })
  }
}

// 格式化JSON
function formatJson() {
  if (lastResponse.value && isJsonResponse.value) {
    try {
      const parsed = JSON.parse(lastResponse.value.data)
      lastResponse.value.data = JSON.stringify(parsed, null, 2)
      showMessage('JSON已格式化', true)
    } catch {
      showMessage('JSON格式无效', false)
    }
  }
}

// 清空请求
function clearRequest() {
  requestMethod.value = 'GET'
  requestUrl.value = ''
  requestHeaders.value = [{ key: 'Content-Type', value: 'application/json' }]
  requestBody.value = ''
  formData.value = [{ key: '', value: '' }]
  bodyType.value = 'json'
  lastResponse.value = null
  showMessage('请求已清空', true)
}

// 保存到历史
function saveToHistory() {
  if (!saveName.value.trim() || !lastResponse.value) return
  
  const requestData = {
    name: saveName.value.trim(),
    alias: saveAlias.value.trim(),
    method: requestMethod.value,
    url: requestUrl.value,
    headers: [...requestHeaders.value],
    body: requestBody.value,
    bodyType: bodyType.value,
    formData: [...formData.value],
    response: { ...lastResponse.value },
    timestamp: Date.now()
  }
  
  savedRequests.value.unshift(requestData)
  saveToLocalStorage()
  
  showSaveDialog.value = false
  saveName.value = ''
  saveAlias.value = ''
  showMessage('API记录已保存', true)
}

// 编辑别名
function editAlias(index: number) {
  editingIndex.value = index
  editingAlias.value = savedRequests.value[index].alias || ''
  showEditDialog.value = true
}

// 更新别名
function updateAlias() {
  if (editingIndex.value >= 0) {
    savedRequests.value[editingIndex.value].alias = editingAlias.value.trim()
    saveToLocalStorage()
    showMessage('别名已更新', true)
  }
  
  showEditDialog.value = false
  editingAlias.value = ''
  editingIndex.value = -1
}

// 从历史加载
function loadFromHistory(item: any) {
  requestMethod.value = item.method
  requestUrl.value = item.url
  requestHeaders.value = [...item.headers]
  requestBody.value = item.body
  bodyType.value = item.bodyType
  formData.value = [...item.formData]
  lastResponse.value = { ...item.response }
  
  showMessage('历史记录已加载', true)
}

// 从历史删除
function deleteFromHistory(index: number) {
  savedRequests.value.splice(index, 1)
  saveToLocalStorage()
  showMessage('记录已删除', true)
}

// 清空历史
function clearHistory() {
  savedRequests.value = []
  saveToLocalStorage()
  showMessage('历史记录已清空', true)
}

// 格式化时间
function formatTime(timestamp: number) {
  return new Date(timestamp).toLocaleString()
}

// 显示消息
function showMessage(msg: string, success: boolean) {
  message.value = msg
  isSuccess.value = success
  setTimeout(() => {
    message.value = ''
  }, 3000)
}

// 本地存储
function saveToLocalStorage() {
  localStorage.setItem('api-tool-history', JSON.stringify(savedRequests.value))
}

function loadFromLocalStorage() {
  const saved = localStorage.getItem('api-tool-history')
  if (saved) {
    try {
      savedRequests.value = JSON.parse(saved)
    } catch {
      savedRequests.value = []
    }
  }
}

// 组件挂载时加载历史记录
onMounted(() => {
  loadFromLocalStorage()
})
</script>

<style scoped>
.api-tool {
  display: flex;
  height: calc(100vh - 40px);
  gap: 20px;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.sidebar {
  width: 300px;
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
  flex-shrink: 0;
}

.main-content {
  flex: 1;
  overflow-y: auto;
}

.request-section, .response-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h3 {
  margin: 0 0 20px 0;
  color: #333;
  border-bottom: 2px solid #007bff;
  padding-bottom: 10px;
}

h4 {
  margin: 15px 0 10px 0;
  color: #555;
  font-size: 16px;
}

/* 请求配置样式 */
.method-url-row {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.method-select {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  min-width: 100px;
}

.url-input {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.send-btn {
  padding: 10px 20px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
}

.send-btn:hover:not(:disabled) {
  background: #0056b3;
}

.send-btn:disabled {
  background: #6c757d;
  cursor: not-allowed;
}

/* Headers样式 */
.header-item, .form-item {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
  align-items: center;
}

.header-key, .header-value, .form-key, .form-value {
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  flex: 1;
}

.remove-btn {
  padding: 8px 12px;
  background: #dc3545;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.remove-btn:hover {
  background: #c82333;
}

.add-btn {
  padding: 8px 16px;
  background: #28a745;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.add-btn:hover {
  background: #218838;
}

/* Body样式 */
.body-type-tabs {
  display: flex;
  gap: 5px;
  margin-bottom: 15px;
}

.tab-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  background: #f8f9fa;
  cursor: pointer;
  border-radius: 4px 4px 0 0;
}

.tab-btn.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

.body-textarea {
  width: 100%;
  min-height: 150px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-family: 'Courier New', monospace;
  resize: vertical;
}

/* 快速操作 */
.quick-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.clear-btn, .sample-btn, .save-btn {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  background: white;
}

.clear-btn:hover {
  background: #f8f9fa;
}

.sample-btn {
  background: #17a2b8;
  color: white;
  border-color: #17a2b8;
}

.sample-btn:hover {
  background: #138496;
}

.save-btn {
  background: #ffc107;
  color: #212529;
  border-color: #ffc107;
}

.save-btn:hover:not(:disabled) {
  background: #e0a800;
}

.save-btn:disabled {
  background: #6c757d;
  color: white;
  cursor: not-allowed;
}

/* 加载动画 */
.loading {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
}

.loading-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid #f3f3f3;
  border-top: 2px solid #007bff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 响应样式 */
.response-status {
  display: flex;
  gap: 15px;
  align-items: center;
  margin-bottom: 15px;
  padding: 12px;
  background: #ffffff;
  border: 1px solid #dee2e6;
  border-radius: 6px;
  text-align: left;
}

.status-code {
  font-weight: bold;
  padding: 4px 8px;
  border-radius: 4px;
  color: white;
}

.status-code.success {
  background: #28a745;
}

.status-code.client-error {
  background: #ffc107;
  color: #212529;
}

.status-code.server-error {
  background: #dc3545;
}

.status-code.info {
  background: #17a2b8;
}

.response-time {
  color: #6c757d;
  font-size: 14px;
}

.headers-display {
  background: #ffffff;
  padding: 15px;
  max-height: 200px;
  overflow-y: auto;
  text-align: left;
}

.header-row {
  margin-bottom: 8px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.5;
  text-align: left;
  color: #212529;
}

.header-name {
  font-weight: bold;
  color: #000000;
}

.response-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.copy-btn, .format-btn {
  padding: 6px 12px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.copy-btn:hover, .format-btn:hover {
  background: #5a6268;
}

.response-text {
  background: #ffffff;
  border: 1px solid #dee2e6;
  padding: 20px;
  border-radius: 6px;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 400px;
  overflow-y: auto;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.6;
  color: #212529;
  text-align: left;
  margin: 0;
}

/* 侧边栏历史记录样式 */
.sidebar h3 {
  margin: 0 0 20px 0;
  color: #333;
  border-bottom: 2px solid #007bff;
  padding-bottom: 10px;
  font-size: 18px;
}

.no-history {
  text-align: center;
  color: #6c757d;
  padding: 20px;
  font-style: italic;
  font-size: 14px;
}

.history-item {
  border: 1px solid #ddd;
  border-radius: 6px;
  padding: 12px;
  margin-bottom: 8px;
  background: #f8f9fa;
  cursor: pointer;
  transition: all 0.2s ease;
}

.history-item:hover {
  background: #e9ecef;
  border-color: #007bff;
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.history-name {
  font-weight: bold;
  color: #333;
  font-size: 14px;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-method {
  background: #007bff;
  color: white;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: bold;
}

.history-url {
  color: #6c757d;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  margin-bottom: 6px;
  word-break: break-all;
  max-height: 32px;
  overflow: hidden;
  line-height: 16px;
}

.history-time {
  color: #6c757d;
  font-size: 11px;
  margin-bottom: 6px;
}

.history-actions {
  display: flex;
  gap: 5px;
  justify-content: flex-end;
}

.edit-btn, .delete-btn {
  padding: 4px 6px;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  background: transparent;
  transition: background 0.2s ease;
}

.edit-btn:hover {
  background: #ffc107;
}

.delete-btn:hover {
  background: #dc3545;
}

.sidebar-actions {
  margin-top: 20px;
  text-align: center;
}

.clear-history-btn {
  padding: 8px 16px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  width: 100%;
}

.clear-history-btn:hover {
  background: #5a6268;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 8px;
  min-width: 400px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.save-name-input, .save-alias-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin: 10px 0;
  font-size: 14px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.confirm-btn {
  padding: 8px 16px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.confirm-btn:hover:not(:disabled) {
  background: #0056b3;
}

.confirm-btn:disabled {
  background: #6c757d;
  cursor: not-allowed;
}

.cancel-btn {
  padding: 8px 16px;
  background: #6c757d;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.cancel-btn:hover {
  background: #5a6268;
}

/* 消息提示 */
.message {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 12px 20px;
  border-radius: 4px;
  color: white;
  font-weight: bold;
  z-index: 1001;
  animation: slideIn 0.3s ease-out;
}

.message.success {
  background: #28a745;
}

.message.error {
  background: #dc3545;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .api-tool {
    flex-direction: column;
    height: auto;
  }
  
  .sidebar {
    width: 100%;
    max-height: 300px;
    order: 2;
  }
  
  .main-content {
    order: 1;
  }
}

@media (max-width: 768px) {
  .api-tool {
    padding: 10px;
  }
  
  .method-url-row {
    flex-direction: column;
  }
  
  .method-select {
    min-width: auto;
  }
  
  .header-item, .form-item {
    flex-direction: column;
  }
  
  .header-key, .header-value, .form-key, .form-value {
    width: 100%;
  }
  
  .quick-actions {
    flex-direction: column;
  }
  
  .modal-content {
    min-width: 90vw;
    margin: 20px;
  }
  
  .sidebar {
    max-height: 200px;
  }
}

/* 跨域说明样式 */
.cors-info {
  margin-top: 15px;
  padding: 12px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 6px;
  border-left: 4px solid #17a2b8;
}

.cors-tip {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  color: #495057;
}

.cors-tip strong {
  color: #17a2b8;
}
</style>